package com.lagou.xsq_springboot_rpc_consumer.consumer;

import com.lagou.xsq_springboot_rpc_common.model.RpcRequest;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.concurrent.Callable;


/**
 * @ClassName: UserClientHandler
 * @Description:
 * @author: xsq
 * @date: 2020年05月25日
 */
public class UserClientHandler extends ChannelInboundHandlerAdapter implements Callable {


    private ChannelHandlerContext context;
    private String result;
    private RpcRequest rpcRequest;

    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        context = ctx;
    }

    /**
     * 收到服务端数据,唤醒等待线程
     */
    @Override
    public synchronized void channelRead(ChannelHandlerContext ctx, Object
            msg) {
        result = msg.toString();
        notify();
    }

    /**
     * 写出数据,开始等待唤醒
     */
    @Override
    public synchronized Object call() throws InterruptedException {
        context.writeAndFlush(rpcRequest);
        wait();
        return result;
    }

    public void setPara(RpcRequest rpcRequest) {
        this.rpcRequest = rpcRequest;
    }
}
